<template>
    <view>
        <view>蓝牙充值</view>
    </view>
</template>

<script>
import BluetoothTool from "./BluetoothTool.js"

let myBlueToothTools = new BluetoothTool()
let mainActivity = plus.android.runtimeMainActivity()
let blueToothInfo = {
  bluetoothStatus: false,
  pairedDevices: [],
  newDevices: [],
  receiveDataArr: [],
  cum: 0
}
export default {
  data() {
    return {
      userNumber: '010811415540',
      userName: '铁木真广场还迁小区18号楼1单元601号司继云',
      chargeMoney: `10元`,
      chargeCount: `46次`,
      deviceId: '54:6C:0E:35:78:18',
      success: '',
      sbyy: '',
      syje: '',
      proValue: 1,
      chargeValue: 1,
      percentage: 15,
      chargeListLength: `1`,
      rightstr: '',
      bwnrJq: ''
    }
  },
  created() {
    // if (myBlueToothTools.isSupportBluetooth()) {
    //   if (myBlueToothTools.getBluetoothStatus()) {
    //     this.getConnetedDevices()
    //   } else {
    //     this.turnOnBluetooth()
    //   }
    // } else {
    //   this.$u.toast("设备不支持蓝牙!")
    // }
    this.step1()
  },
  methods: {
    getConnetedDevices() {
      blueToothInfo.pairedDevices = myBlueToothTools.getConnetedDevices() //获取已经配对的设备
      var searchFlag = true
      for (let index = 0; index < blueToothInfo.pairedDevices.length; index++) {
        const element = blueToothInfo.pairedDevices[index]
        if (this.deviceId == element.address) {
          searchFlag = false
        }
      }
      console.log("getConnetedDevices", searchFlag)
      if (searchFlag) {
        this.findDevices()
      } else {
        this.connDevice(this.deviceId)
      }
    },
    connDevice() {
      console.log("connDevice")
      myBlueToothTools.connDevice(mainActivity, this.deviceId).then(res => {
        console.log("connDevice", res)
        this.step1()
      })
    },
    step1() {
      let address = this.deviceId
      uni.request({
        url: 'http://10.126.29.164:8866/hlwyy/business-mdej/zdcz/sendMessage',
        data: {
          yhdabh: this.userNumber,
          ywbsm: '500601'
        },
        header: {
          'Authorization': 'Basic YWljcF9hcHA6UVd4aFpHUnBianB2Y0dWdUlITmxjMkZ0WlE=',
          "hlwyy-Nonce": "KvFT01IKVumwLkOuB1wCf6BRKK5p2lP0",
          "hlwyy-Token": "2d92e30064ff42d7a19b2976793e8b6f",
          'X-Hci-access-Token': 'YvlJ6GDk4QuUovjN6a70fc8a6100d498',
        }
      }).then(result => {
        console.log("step1===", result[1]);
        let {
          data
        } = result[1]
        let res = data.data
        console.log(data.data)
        if (res.bwnr) {
          myBlueToothTools.sendData(address, res.bwnr)
        } else {
          this.$u.toast(res.sbyy)
        }
      })
    }
  }
}
</script>

<style>

</style>
